package com.sskj.asset.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.asset.bean.CoinEntry;
import com.sskj.asset.bean.ExchangeCoin;
import com.sskj.asset.http.HttpConfig;
import com.sskj.asset.ui.activity.CashActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.CommonUtil;

import java.util.List;


public class CashActivityPresenter extends BasePresenter<CashActivity> {

    /**
     * 获取可兑换币种
     */
    public void getExchangeCoinList() {
        OkGo.<HttpData<List<CoinEntry>>>get(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN_LIST1)
                .params("type",1)
                .execute(new JsonCallBack<HttpData<List<CoinEntry>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<CoinEntry>>> response) {
                        if (response.body().status == 200) {
                            mView.setExchangeAbleList(response.body().getData());
                        } else {
                            ToastUtil.showShort(response.body().getMsg());
                        }
                    }
                });
    }

    /**
     * 可兑换币种列表
     *
     * @param coin
     */
    public void getCoinList(String coin) {
        OkGo.<HttpData<List<CoinEntry>>>get(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN_LIST)
                .params("bid", coin)
                .execute(new JsonCallBack<HttpData<List<CoinEntry>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<CoinEntry>>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.setExchangeCoinData(response.body().getData());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }


    public void exchange(String coin, String ex_coin, String num, String tpwd) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN)
                .params("bid", coin)
                .params("pid", ex_coin)
                .params("num", num)
                .params("tpwd", CommonUtil.getFiveMd5Value(tpwd))
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.submitSuccess(true);
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                            mView.submitSuccess(false);
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                        mView.submitSuccess(false);
                    }
                });
    }
}
